package crossover;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import neural.Individual;

public abstract class AbstractCrossover implements Crossover {

	private List<Integer> randomPositionList = new ArrayList<Integer>();

	private List<Individual> crossAll(List<Individual> individuals) {
		List<Individual> childsAux;
		List<Individual> childs = new ArrayList<Individual>(
				randomPositionList.size());
		// System.out.println(individuals);
		for (int i = 0; i < randomPositionList.size(); i += 2) {
			Individual father = individuals.get(randomPositionList.get(i));
			Individual mother = individuals.get(randomPositionList.get(i + 1));
			//int intFather = IndividualConverter.convertToInt(father);
			//int intMother = IndividualConverter.convertToInt(mother);
			childsAux = generateChilds(father, mother);
			childs.addAll(childsAux);
			//childs.add((childsAux[1]));
		}
		return childs;
	}
	
	@Override
	public List<Individual> cross(List<Individual> individuals){
		randomPositionList.clear();
		int individualsSize = individuals.size();
		for (int i = 0; i < individualsSize; i++) {
			randomPositionList.add(i);
		}
		if (individualsSize % 2 == 1) {
			randomPositionList.remove((int) Math.random() * individualsSize);
		}
		Collections.shuffle(randomPositionList);
		return crossAll(individuals);
	}
	
	protected abstract List<Individual> generateChilds(Individual father, Individual mother);
}
